<?php
/* 
 * Project: oCoder Framework
 * File: /models/user.php
 * Purpose: model for the user controller.
 * Author: Trung Hoang
 */

class baivietModel extends BaseModel
{
    //data passed to the user index view
    public function index($para1)
    {   
        // get all users    

        $baiviets = $this->find_all($table = 'view_bv_baiviet', $orderby = 'id DESC', $where = '', $cols = '*', $limit = ($para1-1)*ROWOFPAGE.','.ROWOFPAGE);
        // get all users
        // $articles = $this->find_all($table = 'articles', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        
        $sobaiviet = $this->count($table = 'bv_baiviet');

        $this->viewModel->set("pageTitle","Danh sách bài viết");
        $this->viewModel->set("baiviets",$baiviets);
        $this->viewModel->set("sobaiviet",$sobaiviet);
        $this->viewModel->set("tranghientai",$para1);
        //var_dump($baiviets);
       // die;
        return $this->viewModel;
    }
    public function category($cate)
    {   
        // get all users    
        $baiviets = $this->find_all($table = 'view_bv_baiviet', $orderby = 'id DESC', $where = 'TuKhoaDanhMuc like"'.$cate.'"', $cols = '*', $limit = '');
        $category = $this->find($table='bv_danhmuc', $orderby = 'iddanhmuc DESC', $where = 'DuongDan like "'.$cate.'"', $cols = '*', $limit = '');
        //var_dump($category);die;
        //$this->viewModel->set("baivietmuonxems",$baivietmuonxems);
        $cothemuonxems = $this->find_all($table = 'view_bv_baiviet', $orderby = 'SoLuotXem DESC', $where = 'TuKhoaDanhMuc like"'.$cate.'"', $cols = '*', $limit = '5');
        $this->viewModel->set("pageTitle",$category->TenDanhMuc);
        $this->viewModel->set("baiviets",$baiviets);
        $this->viewModel->set("category",$category);
        $this->viewModel->set("cothemuonxems",$cothemuonxems);
        return $this->viewModel;
    }
     public function detail($alias)
    {     

        $baiviet = $this->find($table='view_bv_baiviet', $orderby = 'id DESC', $where = 'DuongDan like "'.$alias.'"', $cols = '*', $limit = '');
        $tukhoadanhmuc = $baiviet->TuKhoaDanhMuc;
        $cothemuonxems = $this->find_all($table = 'view_bv_baiviet', $orderby = 'SoLuotXem DESC', $where = 'TuKhoaDanhMuc like"'.$tukhoadanhmuc.'"', $cols = '*', $limit = '5');
        $this->viewModel->set("baiviet",$baiviet);
        $this->viewModel->set("cothemuonxems",$cothemuonxems);
        $this->viewModel->set("pageTitle",$baiviet->TieuDe);
        return $this->viewModel;
    }
    //create model
    public function create($data)
    {   
        //create new item when have data
        if(!empty($data)){
            //return id of item after insert to db
            return $this->save($table = 'bv_baiviet', $data = $data, $where = '');
        }
        $tinhthanhs = $this->find_all($table = 'tinhthanh', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $danhmucs = $this->find_all($table = 'bv_danhmuc', $orderby = 'iddanhmuc DESC', $where = '', $cols = '*', $limit = '');
        
        $this->viewModel->set("tinhthanhs",$tinhthanhs);       
        $this->viewModel->set("danhmucs",$danhmucs);       
        //param var for view
        $this->viewModel->set("pageTitle","Thêm bài viết mới");

        return $this->viewModel;
    }
    //update model
    public function edit($id, $data)
    {
        //update item when have data
        if(!empty($data)){
            //return bool
            return $this->save($table = 'bv_baiviet', $data = $data, $where = 'id='.$id);
        }
        //get item detail by id
        $baiviet = $this->find($table='bv_baiviet', $orderby = 'id DESC', $where = 'id='.$id, $cols = '*', $limit = '');
        $tukhoabaiviet = $baiviet -> TuKhoaBaiViet;
        // get media
        
        $tinhthanhs = $this->find_all($table = 'tinhthanh', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $danhmucs = $this->find_all($table = 'bv_danhmuc', $orderby = 'iddanhmuc DESC', $where = '', $cols = '*', $limit = '');
        $this->viewModel->set("tinhthanhs",$tinhthanhs);       
        $this->viewModel->set("danhmucs",$danhmucs);  
        //param var for view
        $this->viewModel->set("pageTitle","Chỉnh sửa bài viết");
        $this->viewModel->set("baiviet",$baiviet);
        return $this->viewModel;
    }
    public function update_view_article($alias)
    {
        //update item when have data
        if(!empty($alias)){
            //return bool
            return $this->update_view($table = 'bv_baiviet',$where = 'DuongDan like "'.$alias.'"');
        }
    }
}

?>